<?php
/**
 * @file $HeadURL: forum.php $
 * @author $Author: DongJC (dongjianchang@haibao.com) $
 * @date $Date: 2015-4-16 ����7:48:19 $
 * @brief 
 */
namespace haibao\www\data;

class Forum extends \haibao\www\data\BaseMysql{
	
	public function __construct(){
		//parent::__construct ( '\haibao\cms\model\data\Article' );
		//$this->setConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3);
	}
	
	public function addSign($data){
		$this->setConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3);
		$time = time();
		$sql="insert into hb_bussiness_sign_info (uid,sign_time,this_credit) value (%s,%s,%s)";
		$this->query($sql,array($data['userId'],$time,$data['credit']));		
	}
	
	/**
	 * 
	 * @param array $data 用户的数据 id和credit：积分
	 * @param int $day 0为今天，1为昨天，2为前天，依次向之前的日期推算
	 * @return Ambigous <number, NULL, \DateTime>
	 */
	public function isSign($data,$day=0){
		$this->setConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3);
		$isHas = 0;
		$date = date('Y-m-d');
		$starTime = strtotime($date)-86400*$day;
		$endTime = strtotime($date.' 23:59:59')-86400*$day;		
		$sql="select count(sid) as count from hb_bussiness_sign_info where uid = ".$data['userId']." and sign_time>=$starTime and sign_time<= $endTime ";
		$result = $this->query($sql);
		while($result->fetch_assoc()){
			$isHas = $result->getData('count')?$result->getData('count'):0;
		}		
		if($isHas){
			return false;
		}else{
			return true;
		}
	}
	
	public function getWeekSign($userId){
		$this->setConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3);
		$data = array();
		$i = 0;
		$day = date("w");
		$today = date('Y-m-d');
		$statime = strtotime($today)-86400*($day-1);
		$endtime = strtotime($today)+86400*(7-$day);
		$sql="select this_credit,sign_time from hb_bussiness_sign_info where uid = %s and sign_time>='%s' and sign_time<='%s' ";
		$result = $this->query($sql,array($userId,$statime,$endtime));		
		while($result->fetch_assoc()){
			$data[$i]['credit'] = $result->getData('this_credit');
			$data[$i]['signTime'] = $result->getData('sign_time');
			$i++;
		}
		return $data;
	}	
	
}